package com.hhsj.design.pullableview.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.hhsj.design.pullableview.R;
import com.hhsj.design.pullableview.ability.IPullDownAble;
import com.hhsj.design.pullableview.view.IRefreshView;


public class DefaultRefreshView extends ConstraintLayout implements IRefreshView {
    private ProgressBar progressBar;
    private TextView statusTextView;

    public DefaultRefreshView(Context context) {
        this(context, null);
    }

    public DefaultRefreshView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DefaultRefreshView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View.inflate(getContext(), R.layout.layout_default_refreshview, this);
        setBackgroundColor(0x238F2383);
        setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getRefreshHeight()));
        progressBar = findViewById(R.id.progress_bar);
        progressBar.setIndeterminateDrawable(getResources().getDrawable(android.R.drawable.ic_menu_rotate));
        progressBar.setProgressDrawable(getResources().getDrawable(android.R.drawable.ic_menu_rotate));
        statusTextView = findViewById(R.id.refresh_text);
        statusTextView.setText("下拉刷新");
    }

    @Override
    public int getRefreshHeight() {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
    }

    @Override
    public void startAnim() {
        progressBar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.progressbar_cycle));
        progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.progressbar_cycle));
        invalidate();
    }

    @Override
    public void stopAnim() {
        progressBar.setIndeterminateDrawable(getResources().getDrawable(android.R.drawable.ic_menu_rotate));
        progressBar.setProgressDrawable(getResources().getDrawable(android.R.drawable.ic_menu_rotate));
        invalidate();
    }

    @Override
    public void updateText(@NonNull String... text) {
        statusTextView.setText(text[0]);
    }

    @Override
    public void updateWithEventChange(int eventY) {

    }

    @Override
    public void updateWithState(IPullDownAble.HeaderState state) {
        switch (state) {
            case NORMAL:
                updateText("下拉去刷新");
                break;
            case FOR_REFRESH:
                updateText("下拉去刷新");
                break;
            case CAN_REFRESH:
                updateText("松手可刷新");
                break;
            case CAN_CONTINUE:
                updateText("继续下拉有惊喜..");
                break;
            case TO_REFRESH:
                updateText("松手可刷新");
                break;
            case REFRESHING:
                updateText("正在刷新");
                break;
            case TO_NORMAL:
                updateText("下拉去刷新");
                break;
        }
    }

    @Override
    public View getRefreshView() {
        return this;
    }
}
